package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.ClientInfo;
import meijuli.com.join51.business.utils.GlideRoundTransform;
import meijuli.com.join51.business.utils.TimeUtil;

import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的粉丝页面适配器
 *
 * @author wenqin 2017-05-12 15:23
 */

public class ClientListAdapter extends RecyclerView.Adapter<ClientListAdapter.Vh> {

    private LayoutInflater mInflater;
    private List<ClientInfo> mClientInfoList;
    private Context mContext;

    public ClientListAdapter(Context context, List<ClientInfo> clientInfoList) {
        mClientInfoList = clientInfoList;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_client_list, parent, false));
    }

    @Override
    public void onBindViewHolder(Vh holder, int position) {
        ClientInfo clientInfo = mClientInfoList.get(position);
        if (clientInfo == null) {
            return;
        }

        if (!TextUtils.isEmpty(clientInfo.getHeadimg())) {
            Glide.with(mContext).load(clientInfo.getHeadimg())
                    .transform(new GlideRoundTransform(mContext))
                    .error(R.mipmap.icon_header)
                    .into(holder.mHeadImage);
        }

        holder.mNickNameTextView.setText(!TextUtils.isEmpty(clientInfo.getName())
                ? clientInfo.getName() : clientInfo.getPhoneNum());

        if (0 != clientInfo.getCreateTime()) {
            holder.mRegisterTimeTextView.setText(TimeUtil.getFormatTime(clientInfo.getCreateTime(),
                    "yyyy-MM-dd\nHH:mm:ss"));
        } else {
            holder.mRegisterTimeTextView.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mClientInfoList == null ? 0 : mClientInfoList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        CircleImageView mHeadImage;
        TextView mNickNameTextView;
        TextView mRegisterTimeTextView;

        public Vh(View itemView) {
            super(itemView);
            mHeadImage = itemView.findViewById(R.id.headImage);
            mNickNameTextView = itemView.findViewById(R.id.nickNameTextView);
            mRegisterTimeTextView = itemView.findViewById(R.id.registerTimeTextView);
        }
    }
}
